//编辑工作经历
import React, { Component } from 'react'
import {
    View,
    Text,
    TextInput,
    TouchableOpacity,
    ScrollView,
} from 'react-native';

import { getSize } from '../../../../util/sizeUtil'
import { expedit_Style as styles } from '../../../../../styles';
import { posedit_Style as styles1 } from '../../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';
import Autoinput2 from '../../../../util/inputUtil2';
import Timeutil from '../../../../util/newtimeUtil';
import { myAlert } from '../../../../util/compatible';
var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;
var height = Dimensions.get('window').height;

import { fetchRequest } from '../../../../util/fetch';
export default class Expedit_body extends Component {
    constructor(props) {
        super(props);
        this.state = {
            jobExp: {}
        };
    }

    //加载后渲染页面
    componentDidMount() {
        if (!this.state.jobExp.jobexpId && this.props.jobExp.jobexpId) {
            this.setState({ jobExp: this.props.jobExp })
        }
    }

    render() {
        return (
            <View>
                <View style={[styles1.rtopin, {
                    width: width * 1,
                    elevation: 5,
                    shadowColor: 'black',
                    shadowOpacity: 1,
                    shadowRadius: 5
                }]}>

                    <TouchableOpacity activeOpacity={1} style={styles1.rtopicon1} onPress={() => { this.props.navigation.goBack() }}>
                        <Icon
                            name={'ios-arrow-back'}
                            size={25}
                            color={'#ffffff'} />
                    </TouchableOpacity>
                    <Text style={styles1.rtoptext}>工作经历</Text>

                    <TouchableOpacity style={styles1.rtoptextout}
                        onPress={() => {
                            const j = {
                                jobexpId: this.state.jobExp.jobexpId,
                                resumeId: this.props.resume.resumeId,
                                jobexpCompanyName: this.state.jobExp.jobexpCompanyName,
                                fields: this.state.jobExp.fields,
                                jobexpPostion: this.state.jobExp.jobexpPostion,
                                jobexpBeginTime: this.state.jobExp.jobexpBeginTime,
                                jobexpEndTime: this.state.jobExp.jobexpEndTime,
                                jobexpWorkspace: this.state.jobExp.jobexpWorkspace,
                                jobexpMm: this.state.jobExp.jobexpMm,
                                jobexpDuty: this.state.jobExp.jobexpDuty,
                            }
                            if (j.jobexpId) {
                                this.props.actions.updateJobExpAction(j);
                                this.props.navigation.goBack();
                            } else {
                                this.props.actions.InsertJobExpAction(j);
                                this.props.navigation.goBack();
                            }
                        }}
                    >
                        <Text style={styles1.rtoptext2}>保存</Text>
                    </TouchableOpacity>
                </View >
                <View style={{}}>
                    <ScrollView style={styles.rout}
                        ref="sv">

                        {/* 公司名称 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>公司名称</Text>
                            <TextInput
                                style={[styles.inputname, { width: width * 0.7 }]}
                                underlineColorAndroid="transparent"
                                defaultValue={this.state.jobExp.jobexpCompanyName}
                                onChangeText={this.jobexpCompanyName.bind(this)}
                            />
                        </View>

                        {/* 公司领域 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>公司领域</Text>
                            <TouchableOpacity style={[styles.rchooseout, { width: width * 0.7 }]} activeOpacity={1}
                                onPress={() => {
                                    this.props.navigation.navigate('Trade', {
                                        Type: 1, 'rollback': this.setHy.bind(this), list: this.state.jobExp.fields
                                    })
                                }}>
                                <Text style={styles.rchoose}>
                                    {this.fieldName(this.state.jobExp.fields, 3)}
                                </Text>
                            </TouchableOpacity>
                        </View>

                        {/* 职位名称 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>职位名称</Text>
                            <TextInput
                                style={[styles.inputname, { width: width * 0.7 }]}
                                underlineColorAndroid="transparent"
                                defaultValue={this.state.jobExp.jobexpPostion}
                                onChangeText={this.jobexpPostion.bind(this)}
                            />
                        </View>

                        {/* 开始时间 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>开始时间</Text>
                            <Timeutil
                                function={this.jobexpBeginTime.bind(this)}
                                time={this.state.jobExp.jobexpBeginTime}
                            />
                        </View>

                        {/* 结束时间 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>结束时间</Text>
                            <Timeutil
                                function={this.jobexpEndTime.bind(this)}
                                time={this.state.jobExp.jobexpEndTime}
                            />
                        </View>

                        {/* 工作地点 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>工作地点</Text>
                            <TouchableOpacity style={[styles.rchooseout, { width: width * 0.7 }]} activeOpacity={1}
                                onPress={() => {
                                    this.props.navigation.navigate('cityList', {
                                        'callback': this.jobexpWorkspace.bind(this)
                                    });
                                }}>
                                <Text style={styles.rchoose}>{this.state.jobExp.jobexpWorkspace}</Text>
                            </TouchableOpacity>
                        </View>

                        {/* 月薪 */}
                        <View style={styles.rmall}>
                            <Text style={styles.rtxt}>月&emsp;&emsp;薪</Text>
                            <TextInput
                                style={[styles.inputname, { width: width * 0.7 }]}
                                underlineColorAndroid="transparent"
                                keyboardType='numeric'
                                defaultValue={this.NfotS(this.state.jobExp.jobexpMm)}
                                onChangeText={this.jobexpMm.bind(this)}
                            />
                        </View>

                        <View style={styles.rmall2} ref="v1">
                            <Text style={styles.rtxt2}>职责业绩</Text>
                            <Autoinput2
                                style={[styles.inputname, { width: width * 0.7 }]}
                                function={this.jobexpDuty.bind(this)}
                                text={this.state.jobExp.jobexpDuty}
                                onChange={this.gooo.bind(this)}
                                onFocus={this.gooo.bind(this)}
                            />
                        </View>

                        <View style={{ height: 100,alignItems:'flex-end' }}>
                            {this.state.jobExp.jobexpId ?
                                <TouchableOpacity style={styles.del_all} activeOpacity={1} onPress={() => { this._delJobExp(this.state.jobExp.jobexpId) }}>
                                    <Text style={styles.del}>删除</Text>
                                </TouchableOpacity>
                                :
                                null
                            }
                        </View>

                        {/* 职责业绩 */}
                    </ScrollView >
                </View>
            </View>
        )
    }

    _delJobExp(jobExpId) {
        fetchRequest('JobExp/deleteJobExpMob.go', 'POST', { jobExpId: jobExpId }).then(ret => {
            if (ret.msg == 'ok') {
                this.props.actions.EditResumeAction();
                this.props.navigation.goBack();
            } else {
                myAlert('删除失败')
            }
        })
    }

    //公司名称
    jobexpCompanyName(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpCompanyName = a;
        this.setState({ jobExp: jobExp });
    }


    //经营领域
    fieldName(List, sl) {
        let arr = '';
        if (List != undefined) {
            let s = 1;
            let length = List.length;
            for (let i = 0; i < List.length; i++) {
                if (List[i].fieldType == '1' && s <= sl) {
                    arr += List[i].fieldName + '/';
                    ++s;
                }
            }
            if (arr.endsWith('/')) {
                arr = arr.substr(0, arr.length - 1);
            }
        }
        return arr;
    }

    setHy(val, type) {
        const jobExp = this.state.jobExp;
        if (this.state.jobExp.fields!=undefined){
            for (let i = 0; i < this.state.jobExp.fields.length; i++) {
                if (this.state.jobExp.fields[i].fieldType == type) {
                    this.state.jobExp.fields.splice(i, 1)
                    i = -1;
                }
            }
            jobExp.fields = jobExp.fields.concat(val)
        }else{
            jobExp.fields=val
        }
        this.setState({
            jobExp: jobExp
        })
    }

    //职位名称
    jobexpPostion(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpPostion = a;
        this.setState({ jobExp: jobExp });
    }

    //开始时间
    jobexpBeginTime(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpBeginTime = a;
        this.setState({ jobExp: jobExp });
    }

    //结束时间
    jobexpEndTime(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpEndTime = a;
        this.setState({ jobExp: jobExp });
    }

    //工作地点
    jobexpWorkspace(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpWorkspace = a;
        this.setState({ jobExp: jobExp });
    }

    //月薪
    jobexpMm(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpMm = a;
        this.setState({ jobExp: jobExp });
    }

    //职责业绩
    jobexpDuty(a) {
        const jobExp = this.state.jobExp;
        jobExp.jobexpDuty = a;
        this.setState({ jobExp: jobExp });
    }
    gooo() {
        getSize(this.refs.v1).then(res => {
            //console.error(res)
            this.refs.sv.scrollTo({ x: 0, y: (res.height + res.pageY), animated: true })
        })
    }

    //数字转换
    NfotS(num) {
        let s = '';
        if (num != undefined) {
            s += num + '';
        }else{
            s='0';
        }
        return s;
    }
}